<template>
  <a-modal
    title="编辑友情链接"
    :width="900"
    :visible="visible"
    :confirmLoading="confirmLoading"
    @ok="handleSubmit"
    @cancel="handleCancel"
  >
    <a-spin :spinning="confirmLoading">
      <a-form :form="form">
        <a-form-item v-show="false">
          <a-input v-decorator="['linkId']"/>
        </a-form-item>
        <a-form-item
          label="友链类别"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
        >
          <a-radio-group
            placeholder="请选择友链类别"
            v-decorator="['linkType',{rules: [{ required: true, message: '请选择友链类别！' }]}]">
            <a-radio v-for="(item,index) in linkTypeData" :key="index" :value="parseInt(item.code)">{{
              item.name
            }}
            </a-radio>
          </a-radio-group>
        </a-form-item>
        <a-form-item
          label="网站名称"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          has-feedback
        >
          <a-input
            placeholder="请输入网站名称"
            v-decorator="['linkName', {rules: [{required: true, message: '请输入网站名称！'}]}]"/>
        </a-form-item>
        <a-form-item
          label="网站链接"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          has-feedback
        >
          <a-input
            placeholder="请输入网站链接"
            v-decorator="['linkUrl', {rules: [{required: true, message: '请输入网站链接！'}]}]"/>
        </a-form-item>
        <a-form-item
          label="网站描述"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          has-feedback
        >
          <a-input
            placeholder="请输入网站描述"
            v-decorator="['linkDescription', {rules: [{required: true, message: '请输入网站描述！'}]}]"/>
        </a-form-item>
        <a-form-item
          label="用于列表排序"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          has-feedback
        >
          <a-input
            placeholder="请输入用于列表排序"
            v-decorator="['linkRank', {rules: [{required: true, message: '请输入用于列表排序！'}]}]"/>
        </a-form-item>
        <a-form-item
          label="是否删除"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
        >
          <a-radio-group
            placeholder="请选择是否删除"
            v-decorator="['isDeleted',{rules: [{ required: true, message: '请选择是否删除！' }]}]">
            <a-radio v-for="(item,index) in isDeletedData" :key="index" :value="parseInt(item.code)">{{
              item.name
            }}
            </a-radio>
          </a-radio-group>
        </a-form-item>
      </a-form>
    </a-spin>
  </a-modal>
</template>

<script>
import { blogLinkEdit } from '@/api/modular/main/bloglink/blogLinkManage'

export default {
  data() {
    return {
      labelCol: {
        xs: { span: 24 },
        sm: { span: 5 }
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 15 }
      },
      linkTypeData: [],
      isDeletedData: [],
      visible: false,
      confirmLoading: false,
      form: this.$form.createForm(this)
    }
  },
  methods: {
    // 初始化方法
    edit(record) {
      this.visible = true
      const linkTypeOption = this.$options
      this.linkTypeData = linkTypeOption.filters['dictData']('dict_link_type')
      const isDeletedOption = this.$options
      this.isDeletedData = isDeletedOption.filters['dictData']('dict_is_deleted')
      setTimeout(() => {
        this.form.setFieldsValue(
          {
            linkId: record.linkId,
            linkType: record.linkType,
            linkName: record.linkName,
            linkUrl: record.linkUrl,
            linkDescription: record.linkDescription,
            linkRank: record.linkRank,
            isDeleted: record.isDeleted
          }
        )
      }, 100)
    },
    handleSubmit() {
      const { form: { validateFields } } = this
      this.confirmLoading = true
      validateFields((errors, values) => {
        if (!errors) {
          for (const key in values) {
            if (typeof (values[key]) === 'object') {
              values[key] = JSON.stringify(values[key])
            }
          }
          blogLinkEdit(values).then((res) => {
            if (res.success) {
              this.$message.success('编辑成功')
              this.confirmLoading = false
              this.$emit('ok', values)
              this.handleCancel()
            } else {
              this.$message.error('编辑失败')//  + res.message
            }
          }).finally((res) => {
            this.confirmLoading = false
          })
        } else {
          this.confirmLoading = false
        }
      })
    },
    handleCancel() {
      this.form.resetFields()
      this.visible = false
    }
  }
}
</script>
